class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (prices.size() <= 1) {
            return 0;
        }

        int n = prices.size();
        int ans = 0;
        for (int i = 0; i < n; ) {
            int j = i + 1;
            while (j < n && prices[j] >= prices[j - 1]) {
                ++j;
            }
            ans += prices[j - 1] - prices[i];
            i = j;
        }
        return ans;
    }
};
